package models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import play.Logger;
import play.data.validation.Required;
import siena.Filter;
import siena.Query;

public class InformationRequest extends AbstractActivity {
	@Required public String description;
	
	@Filter("informationRequest")
	public Query<InformationUnit> informations;
	
	public static Query<InformationRequest> all() {
		return all(InformationRequest.class);
	}
	
	public static Query<InformationRequest> findById(Long id, Query<InformationRequest> query) {
		if(query == null) query = all();
		
		return query.filter("id", id);
	}
	
	public static InformationRequest getById(Long id, Query<InformationRequest> query) {
		return findById(id, query).get();
	}
	
	public static List<InformationRequest> findAllOpen(Date date, Query<InformationRequest> query) {
		if(query != null) query = query.filter("start<", date);
		else query = all().filter("start<", date);
				
		return filterByEndDate(query.fetch(), date, true);
	}
	
	public static List<InformationRequest> findAllClose(Date date, Query<InformationRequest> query) {
		if(query != null) query = query.filter("start<", date);
		else query = all().filter("start<", date);
		
		return filterByEndDate(query.fetch(), date, false);
	}
	
	public static List<InformationRequest> findAllRemaining(Date date, Query<InformationRequest> query) {
		List<InformationRequest> infoRequests = null;
		if(query != null) infoRequests = query.filter("start>", date).fetch();
		else infoRequests = all().filter("start>", date).fetch();
		
		return filterByEndDate(infoRequests, date, true);
	}
	
	public static List<InformationRequest> findByStartDate(Date startDate, Date endDate, Query<InformationRequest> query) {
		List<InformationRequest> infoRequests = null;
		if(query == null) query = all();
		infoRequests =  query.filter("start>=", startDate).filter("start<=", endDate).fetch();
		
		return infoRequests;
	}
	
	public static List<InformationRequest> findByEndDate(Date startDate, Date endDate, Query<InformationRequest> query) {
		List<InformationRequest> infoRequests = null;
		if(query == null) query = all();
		infoRequests = query.filter("end>=", startDate).filter("end<=", endDate).fetch();
		
		return infoRequests;
	}
	
	/**
	 * Manual filter by the ending fate. Because now datastore don't allow inequality filters on more than one property.
	 * @param list 
	 * @param end
	 * @param flag Indicates if the end date has to be greater (true) than the incoming date o lower (false).
	 * @return
	 */
	private static  List<InformationRequest> filterByEndDate(List<InformationRequest> list, Date date, boolean flag) {
		ArrayList<InformationRequest> result = new ArrayList<InformationRequest>();
		if(list != null) {
			for(InformationRequest ir : list) {
				if(flag) {
					if(ir.end.getTime() >= date.getTime()) result.add(ir);
				} else {
					if(ir.end.getTime() <= date.getTime()) result.add(ir);
				}
				
			}
		}
		
		return result;
	}
}
